<?php

namespace app\api\controller;

use app\admin\model\invest\Config;
use app\admin\model\District;
use app\admin\model\money\Change;
use app\admin\model\user\Order;
use app\common\controller\Api;
use app\common\model\Area;
use think\Db;
use think\Request;

/**
 * 地址接口
 */
class Userorder extends Api
{
    protected $noNeedLogin = '';
    protected $noNeedRight = ['*'];

    protected $model = null;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->model = new Order();
    }

    /**
     * 获取订单列表
     * @ApiSummary  (WanlShop 订单接口获取订单列表)
     * @ApiMethod   (GET)
     *
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $list = $this->model
            ->where('user_id', $this->auth->id)
            ->order('id desc')
            ->select();

        if ($list) {
            foreach ($list as $val) {
                $images_arr = explode(',', $val->images);
                if ($images_arr) {
                    $images_arr = array_map('make_full_url', $images_arr);
                    $val->images = $images_arr;
                }

                $val->createtime = date('Y-m-d H:i:s', $val->createtime);
            }
        }

        $this->success('订单列表为', $list);
    }

    /**
     * 上传图片
     * @throws \think\Exception
     */
    public function setImg()
    {
        if (!$this->request->isPost()) {
            $this->error('非法操作');
        }
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $id = $this->request->post('id');
        $images = $this->request->post('images');

        if (!$images) {
            $this->error('参数缺失！');
        }

        $user_id = $this->auth->id;
        if ($id) {
            $row = $this->model->where(['user_id' => $user_id])->find($id);

            if (!$row) {
                $this->error('该订单不存在！');
            }
            if ($row->status != 0) {
                $this->error('该订单已处理过！');
            }

            $row->status = 1;
            $row->images = $images;
            $res = $row->save();
        } else {
            $odata = [
                'images' => $images,
                'user_id' => $user_id,
                'status' => 1
            ];
            $res = $this->model->create($odata);
        }

        if (!$res) {
            $this->error('上传失败！');
        }
        $this->success('上传成功');

    }

    /**
     * 订单详情
     */
    public function read()
    {
        if (!$this->request->isPost()) {
            $this->error('非法操作');
        }
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $id = $this->request->post('id');
        if (!$id) {
            $this->error('参数错误');
        }
        $userId = $this->auth->id;
        $row = $this->model->where('id', $id)->where('user_id', $userId)->find();
        if (!$row) {
            $this->error('该订单不存在!');
        }

        $this->success('订单详情为', $row);
    }

    /**
     * 一键参与
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function makeInvests()
    {
        $id = input('id');
        if (!$id) {
            $this->error('参数缺失！');
        }

        $userId = $this->auth->id;
        $row = $this->model->where('id', $id)->where('user_id', $userId)->find();
        if (!$row) {
            $this->error('该订单不存在!');
        }

        if ($row['type'] != 1) {
            $this->error('不可参与！');
        }

        if ($row['status'] != 2) {
            $this->error('不可参与！');
        }

        // 启动事务
        Db::startTrans();
        try {

            $config = Config::where('status', 'normal')->select();
            foreach ($config as $val) {
                $money = round($row['give_money'] * $val->rate1, 2);
                $idata = [
                    'user_id' => $row['user_id'],
                    'order_id' => $row['id'],
                    'money' => $money,
                    'days' => $val->days,
                    'rate' => $val->rate2,
                    'num' => 1,
                    'status' => 0
                ];

                \app\admin\model\user\Invest::create($idata);
            }

            $row->status = 3;
            $row->save();

            $finance = \app\admin\model\user\Finance::where('user_id', $userId)->find();
            $finance->hasscore -= $row['give_money'];
            $finance->save();

            // 流水
            $orderNum = create_order($userId);
            $mdata1 = [
                'order_num' => $orderNum,
                'record_type' => 0,
                'change_type' => 10,
                'moneytype' => 1,
                'money' => $row['give_money'],
                'hasmoney' => $finance->hasscore,
//                'remark' => $tag,
                'user_id' => $userId,
                'targetuserid' => $userId
            ];

            Change::create($mdata1);

            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error('参与失败！');
        }

        $this->success('生成投资订单');
    }
}
